﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SQLQuery.Common;

namespace SQLQuery.UI.Comparsion
{
    /// <summary>
    /// DataColumnCustomWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataColumnCustomWindow : Window
    {
        public DataColumnCustomWindow(DataFilterColumnEntity[] table)
        {
            InitializeComponent();
            CustomColumn = new Dictionary<string, string>();
            InitControl(table);
            XmlHelper.Instance.SetTheme(this);
        }

        private void InitControl(DataFilterColumnEntity[] table)
        {
            this.grid.Children.Clear();
            //N行
            for (int i = 0; i < table.Length; i++)
            {
                this.grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(35) });
            }

            for (int i = 0; i < table.Length; i++)
            {
                DataFilterColumnEntity entity = table[i];
               DockPanel dp = GetItemElement(
                    entity,
                    i );
                this.grid.Children.Add(dp);
            }
        }

        private DockPanel GetItemElement(DataFilterColumnEntity entity, int rowindex)
        {
            DockPanel panel = new DockPanel();
            Label label = new Label { Content = entity.ColumnName, MinWidth = 100, MaxWidth = 100, Margin = new Thickness(5, 5, 2, 5) };
            panel.Children.Add(label);
            if (entity.Primary == "PK")
            {
                label.Foreground = (Brush)new BrushConverter().ConvertFromString("#FFC45B5B");
                ComboBox cb = new ComboBox();
                cb.Margin = new Thickness(0, 5, 5, 5);
                cb.Items.Add("[GUID]");
                cb.Items.Add("[DATE]");
                cb.Items.Add("[原始值]");
                cb.Items.Add("[目标值]");
                panel.Children.Add(cb);
            }
            else
            {
                TextBox tb = new TextBox { Margin = new Thickness(0, 5, 5, 5) };
                panel.Children.Add(tb);
            }
           
            panel.SetValue(Grid.RowProperty, rowindex);
            return panel;
        }

        public Dictionary<string, string> CustomColumn { get; set; }
        private void BtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (DockPanel dp in this.grid.Children)
                {
                    string fieldname = (dp.Children[0] as Label).Content.ToString();
                    string valueStr = string.Empty;
                    if (dp.Children[1] is ComboBox)
                    {
                        valueStr = (dp.Children[1] as ComboBox).Text;
                    }
                    else
                    {
                        valueStr = (dp.Children[1] as TextBox).Text;
                    }
                    CustomColumn.Add(fieldname, valueStr);
                }
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                SQLQuery.Common.MessageBoxHelper.Show("失败,详细:" + ex.Message);
            }
        }

        private void BtnCanel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
